package com.fescoservice.service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fescoservice.dao.BBSAttachDAO;
import com.fescoservice.dao.BBSContentDAO;
import com.fescoservice.dao.BBSDAO;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.BBS;
import com.fescoservice.model.BBSAttach;
import com.fescoservice.model.BBSAttachId;
import com.fescoservice.model.BBSContent;
import com.fescoservice.model.BBSId;
import com.fescoservice.model.CategoryId;

@Service
public class ContentService {
	private static final Logger log = LogManager.getLogger(ContentService.class);
	
	@Autowired
	private BBSDAO bbsDAO;
	@Autowired
	private BBSContentDAO bbsContentDAO;
	@Autowired
	private BBSAttachDAO bbsAttachDAO;
	
	public void addContent(BBSId bbsId ,BBS bbs,BBSContent content,List<MultipartFile> multipartFiles,File filePath) throws FSException{
		try {
			bbsDAO.save(bbs);
			bbsContentDAO.save(content);
			BBSAttachId id =null;
			String extName = null;
			String fileName = null;
			File file = null;
			BBSAttach att = null;
			Long index = 1L;
			SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSS");
			for(MultipartFile multipartFile : multipartFiles){
				if(multipartFile.isEmpty()) continue;
				extName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
				fileName =  "File"+df2.format((new Date()).getTime())+extName;   
				file = new File(filePath + File.separator +fileName);  
				multipartFile.transferTo(file);  
				id = new BBSAttachId(bbsId.getBoardCode1(),bbsId.getBoardCode2(),bbsId.getBoardCode3(),bbsId.getBoardSeq(),index++);
				att = new BBSAttach(id, filePath+"", fileName ,multipartFile.getSize(), multipartFile.getOriginalFilename(), multipartFile.getContentType()) ;
				bbsAttachDAO.save(att);
				log.debug("~~~~~~~~Controller deleteCategory filePath : ~~~~~~~~"+fileName);
			}
		} catch (IllegalStateException e) {
			throw new FSException("FS_ERROR_FILESAVE",e.getMessage());
		} catch (IOException e) {
			throw new FSException("FS_ERROR_FILESAVE",e.getMessage());
		}
	}	
	
	public List findBBSByCategory(CategoryId id ) throws FSException{
		String hql = "from BBS b where  b.id.boardCode1 = :boardCode1 and b.id.boardCode2 = :boardCode2 and b.id.boardCode3 = :boardCode3 order by b.defaultYn desc";
		return bbsDAO.findBBSByCategory(hql,id);
	}
	
	public void setDefault(BBSId id) throws FSException{				
		String hql = " update BBS b set b.defaultYn = 'N' where b.id.boardCode1 = :boardCode1 and b.id.boardCode2 = :boardCode2 and b.id.boardCode3 = :boardCode3 ";
		bbsDAO.updateDefault(hql,id.getBoardCode1(),id.getBoardCode2(),id.getBoardCode3());
		BBS bbs = bbsDAO.findById(id);		
		bbs.setDefaultYn("Y");
		bbsDAO.update(bbs);	
	}	
	
	public void deleteContent(BBSId id) throws FSException{		
		String hql = " b where b.id.boardCode1 = :boardCode1 and b.id.boardCode2 = :boardCode2 and b.id.boardCode3 = :boardCode3 and b.id.boardSeq = :boardSeq";
		
		log.debug("~~~~~~~~deleteContent Service begin~~~~~~~~"+id.getBoardCode1());
		log.debug("~~~~~~~~deleteContent Service begin~~~~~~~~"+id.getBoardCode2());
		log.debug("~~~~~~~~deleteContent Service begin~~~~~~~~"+id.getBoardCode3());
		bbsDAO.deleteBBS("delete from BBS"+hql,id);	
		bbsDAO.deleteBBS("delete from BBSContent"+hql,id);	
		bbsDAO.deleteBBS("delete from BBSAttach"+hql,id);
	}	
	
	public Long countBBSId(BBSId id) throws FSException{
		String sql = "SELECT COUNT(*) AS CNT FROM T_BBS B WHERE B.BOARD_CODE1 = :boardCode1 AND B.BOARD_CODE2 = :boardCode2 AND B.BOARD_CODE3 = :boardCode3";
		return bbsDAO.countBBSId(sql,id);
	}
	
	public BBS findBBSById(BBSId id) {
		return bbsDAO.findById(id);
	}
	
	public BBSContent findBBSContentById(BBSId id) {
		return bbsContentDAO.findById(id);
	}
	
	public void updateContent(BBS bbs,BBSContent c,List<MultipartFile> multipartFiles,File filePath) throws FSException{
		try {
			bbsDAO.update(bbs);
			bbsContentDAO.update(c);
			BBSId bbsId = bbs.getId();
			BBSAttachId id =null;
			String extName = null;
			String fileName = null;
			File file = null;
			BBSAttach att = null;
			String sql = "SELECT COUNT(*) AS CNT FROM T_BBS_ATTACH B WHERE B.BOARD_CODE1 = :boardCode1 AND B.BOARD_CODE2 = :boardCode2 AND B.BOARD_CODE3 = :boardCode3 AND B.BOARD_SEQ = :boardSeq";
			Long index =  bbsAttachDAO.countBBSAttachId(sql,bbsId);
			SimpleDateFormat df2 = new SimpleDateFormat("yyyyMMddHHmmssSS");
			for(MultipartFile multipartFile : multipartFiles){
				if(multipartFile.isEmpty()) continue;
				extName = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
				fileName =  "File"+df2.format((new Date()).getTime())+extName;   
				file = new File(filePath + File.separator +fileName);  
				multipartFile.transferTo(file);  
				id = new BBSAttachId(bbsId.getBoardCode1(),bbsId.getBoardCode2(),bbsId.getBoardCode3(),bbsId.getBoardSeq(),++index);
				att = new BBSAttach(id, filePath+"", fileName ,multipartFile.getSize(), multipartFile.getOriginalFilename(), multipartFile.getContentType()) ;
				bbsAttachDAO.save(att);
				log.debug("~~~~~~~~Controller deleteCategory filePath : ~~~~~~~~"+fileName);
			}
		} catch (IllegalStateException e) {
			throw new FSException("FS_ERROR_FILESAVE",e.getMessage());
		} catch (IOException e) {
			throw new FSException("FS_ERROR_FILESAVE",e.getMessage());
		}
	}
	
	public List findBBSAttachesById(BBSId id) throws FSException{
		String hql = "from BBSAttach b where  b.id.boardCode1 = :boardCode1 and b.id.boardCode2 = :boardCode2 and b.id.boardCode3 = :boardCode3 and b.id.boardSeq = :boardSeq";
		return bbsAttachDAO.findBBSAttachesById(hql,id);
	}
}
